package com.cqcet.wenchuang.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cqcet.wenchuang.common.R;
import com.cqcet.wenchuang.common.customException.CustomException;
import com.cqcet.wenchuang.dto.ShoppingCartDto;
import com.cqcet.wenchuang.entity.Goods;
import com.cqcet.wenchuang.entity.ShoppingCart;
import com.cqcet.wenchuang.service.GoodsService;
import com.cqcet.wenchuang.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 购物车
 */
@Slf4j
@Api(tags = "购物车管理接口")
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private GoodsService goodsService;

    /**
     * 新增
     * @param shoppingCart
     * @return
     */
    @ApiOperation("添加购物车")
    @PostMapping
    public R<String> save(@RequestBody ShoppingCart shoppingCart) {

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        //根据用户名查询购物车
        queryWrapper.eq(ObjectUtils.isNotEmpty(shoppingCart.getUserId()), ShoppingCart::getUserId, shoppingCart.getUserId());
        //根据商品查询商品
        queryWrapper.eq(ObjectUtils.isNotEmpty(shoppingCart.getGoodsId()), ShoppingCart::getGoodsId, shoppingCart.getGoodsId());
        ShoppingCart one = shoppingCartService.getOne(queryWrapper);
        //获取购物车中商品的信息
        Goods goods = goodsService.getById(shoppingCart.getGoodsId());
        if (ObjectUtils.isNotEmpty(one)) {
            int currentQuantity = one.getQuantity() + shoppingCart.getQuantity();
            if (goods.getQuantity() < currentQuantity) {
                throw new CustomException("商品数量不足,添加失败");
            }
            one.setQuantity(currentQuantity);
            shoppingCartService.updateById(one);
        } else {
            if (goods.getQuantity() < shoppingCart.getQuantity()) {
                throw new CustomException("商品数量不足,添加失败");
            }
            shoppingCartService.save(shoppingCart);
        }
        return R.success("添加成功");
    }

    /**
     * 删除单条
     * @param id
     * @return
     */
    @ApiOperation("删除购物车")
    @DeleteMapping("/{id}")
    public R<String> remove(@PathVariable Long id) {
        shoppingCartService.removeById(id);
        return R.success("删除成功");
    }

    /**
     * 修改
     * @param shoppingCart
     * @return
     */
    @ApiOperation("修改购物车")
    @PutMapping
    public R<String> update(@RequestBody ShoppingCart shoppingCart) {
        //获取购物车中商品的信息
        Goods goods = goodsService.getById(shoppingCart.getGoodsId());
        if (goods.getQuantity() < shoppingCart.getQuantity()) {
            throw new CustomException("商品数量不足,修改失败");
        }
        shoppingCartService.updateById(shoppingCart);
        return R.success("修改成功");
    }

    /**
     * 获取所有
     * @param userId
     * @return
     */
    @ApiOperation("用户全部购物车")
    @GetMapping("/{userId}")
    public R<List<ShoppingCartDto>> list(@PathVariable Long userId) {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        //根据用户id查询所有
        queryWrapper.eq(ShoppingCart::getUserId, userId);

        //根据修改时间，创建时间降序排列
        queryWrapper.orderByDesc(ShoppingCart::getUpdateTime).orderByDesc(ShoppingCart::getCreateTime);

        List<ShoppingCart> shoppingCartList = shoppingCartService.list(queryWrapper);
        //将商品信息封装到dto对象
        List<ShoppingCartDto> collect = shoppingCartList.stream().map((shoppingCart) -> {
            ShoppingCartDto shoppingCartDto = new ShoppingCartDto();
            //购物车信息
            BeanUtils.copyProperties(shoppingCart, shoppingCartDto);
            //商品信息
            Goods goods = goodsService.getById(shoppingCart.getGoodsId());
            shoppingCartDto.setGoods(goods);
            return shoppingCartDto;
        }).collect(Collectors.toList());

        return R.success(collect);
    }
}
